{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "534c1e0b",
   "metadata": {},
   "source": [
    "# 第十三周 学习笔记\n",
    "\n",
    "----------\n",
    "\n",
    "* 笔记内容：集合\n",
    "* 记录人：蔡洪嘉\n",
    "* 时间：2021-11-25\n",
    "* Week13\n",
    "\n",
    "* 主要参考文档:\n",
    "> 1. [Python官方文档](https://python/org)\n",
    "> 2. [许老师博客](https://cnblogs.com/xuzhichao)\n",
    "\n",
    "----------\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "beb2dad0",
   "metadata": {},
   "source": [
    "# 上周回顾\n",
    "\n",
    "## Json（文本数据） ---> Dict（python中的字典数据）\n",
    "\n",
    "## 字典和列表的综合实践\n",
    "\n",
    "--------------\n",
    "\n",
    "# 本周内容\n",
    "\n",
    "## [集合]()\n",
    "\n",
    "* 思考：我想了解一组数据中有哪些唯一存在的值？（假如是列表）\n",
    "* list_age = [18,24,25,24,18,23,18]\n",
    " \n",
    "> 1. 集合的性质  \n",
    ">> 1. 无序  \n",
    ">> 2. 不重复  \n",
    "> 2. 集合的一般方法  \n",
    ">> 1. 增加   \n",
    ">> 2. 删除  \n",
    ">> 3. 查询：in查询成员关系    \n",
    "> 3. 集合的数学逻辑运算  \n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a143e2bd",
   "metadata": {},
   "source": [
    "### 集合\n",
    ">- 集合（set）是一个无序的不重复元素序列。\n",
    ">\n",
    ">- 可以使用大括号 { } 或者 set() 函数创建集合，注意：创建一个空集合必须用 set() 而不是 { }，因为 { } 是用来创建一个空字典。\n",
    "\n",
    "> 创建格式：\n",
    ">- parame = {value01,value02,...}\n",
    ">- 或者\n",
    ">- set(value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "3bc6851b",
   "metadata": {},
   "outputs": [],
   "source": [
    "no_duplicate_age = []\n",
    "list_age = [18,24,25,24,18,23,18]\n",
    "for i in list_age:\n",
    "    if i not in no_duplicate_age:\n",
    "        no_duplicate_age.append(i)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "9fae8c76",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[18, 24, 25, 23]\n"
     ]
    }
   ],
   "source": [
    "print(no_duplicate_age)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1503e02c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{24, 25, 18, 23}\n"
     ]
    }
   ],
   "source": [
    "# list ---> set的方法：set()\n",
    "no_duplicate_age_set = set(list_age)\n",
    "print(no_duplicate_age_set)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "8e9023ed",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'apple', 'banana', 'pear', 'orange'}\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# 官方文档测试代码\n",
    "# 1. 移除重复项\n",
    "basket = {'apple','orange','apple','pear','orange','banana'}\n",
    "print(basket)\n",
    "# 2. in 查询成员关系\n",
    "print('orange'in basket)\n",
    "print('crabgrass'in basket)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "37d8f34f",
   "metadata": {},
   "source": [
    "### 集合的一般方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d6f55f49",
   "metadata": {},
   "source": [
    "> * 1. 集合的创建的两种一般方法：\n",
    ">> 1. set(list)\n",
    ">> 2. 直接按照集合的结构创建：{value1,value2,value3,...}\n",
    "> * 2. 增加add()\n",
    ">> 1. 结果：Add an element to a set\n",
    ">> 2. 注意：This has no effect if the element is already present.\n",
    "> * 3. 删除方法\n",
    ">> 1. pop():Remove and return an arbitrary set element.Raises KeyError if the set is empty.\n",
    ">> 2. remove()\n",
    ">> 3. discard()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f683daf8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 66, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "set_1 = set([1,3,4,0,8,66,7])\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "9a768bb9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function update:\n",
      "\n",
      "update(...) method of builtins.set instance\n",
      "    Update a set with the union of itself and others.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set_1.update)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "8aec53e4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 66, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "set_1.add(66)\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "8c3b0585",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(...) method of builtins.set instance\n",
      "    Remove and return an arbitrary set element.\n",
      "    Raises KeyError if the set is empty.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set_1.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "0fac2c12",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 66, 3, 4, 7, 8}\n",
      "{1, 66, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "print(set_1)\n",
    "set_1.pop()\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "95d70222",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 66, 3, 4, 7, 8}\n",
      "{1, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "print(set_1)\n",
    "set_1.remove(66)\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "751ef16a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# * 避免KeyError的方式：\n",
    "if 66 in set_1:\n",
    "    set_1.remove(66)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "18916945",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "print(set_1)\n",
    "set_1.discard(66)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "868c05fa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(...) method of builtins.set instance\n",
      "    Remove an element from a set; it must be a member.\n",
      "    \n",
      "    If the element is not a member, raise a KeyError.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set_1.remove)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "225806a9",
   "metadata": {},
   "source": [
    "### 3. 集合的运算\n",
    "\n",
    "> 1. 集合的交集\n",
    "> 2. 集合的并集\n",
    "> 3. 集合的差集"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "efbd57ef",
   "metadata": {},
   "source": [
    "> 1. 集合的交集\n",
    ">> 1. 使用集合的方法：set.intersection()\n",
    ">> 2. set_1 & set_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "a3608b09",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1： {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2： {0, 1, 66, 3, 4, 7, 8}\n",
      "交集结果： {1, 3, 4, 7}\n",
      "交集结果： {1, 3, 4, 7}\n"
     ]
    }
   ],
   "source": [
    "set_1 = set([1,2,5,6,7,2,3,2,4,4])\n",
    "set_2 = set([1,3,4,0,8,66,7])\n",
    "print(\"set_1：\",set_1)\n",
    "print(\"set_2：\",set_2)\n",
    "print(\"交集结果：\",set_1.intersection(set_2))\n",
    "print(\"交集结果：\",set_1&set_2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a46b771",
   "metadata": {},
   "source": [
    "> 2. 集合的并集\n",
    ">> 1. 使用并集的方法：set_1.union(set_2)\n",
    ">> 2. 使用逻辑运算符：set_1 | set_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "38056334",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1： {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2： {0, 1, 66, 3, 4, 7, 8}\n",
      "交集结果： {0, 1, 2, 3, 4, 5, 6, 7, 66, 8}\n",
      "交集结果： {0, 1, 2, 3, 4, 5, 6, 7, 66, 8}\n"
     ]
    }
   ],
   "source": [
    "print(\"set_1：\",set_1)\n",
    "print(\"set_2：\",set_2)\n",
    "print(\"交集结果：\",set_1.union(set_2))\n",
    "print(\"交集结果：\",set_1|set_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "1a4cac1d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "开始的时间： 1637811004.896531\n",
      "set_1: {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2: {0, 1, 66, 3, 4, 7, 8}\n",
      "交集结果： {0, 1, 2, 3, 4, 5, 6, 7, 66, 8}\n",
      "结束的时间： 1637811004.897551\n",
      "运行的时间： 0.0010199546813964844\n"
     ]
    }
   ],
   "source": [
    "# 计算运行效率 = 运行后的时间节点 - 运行前的时间节点\n",
    "import time\n",
    "start_time = time.time()\n",
    "print(\"开始的时间：\",start_time)\n",
    "print(\"set_1:\",set_1)\n",
    "print(\"set_2:\",set_2)\n",
    "print(\"交集结果：\",set_1|set_2)\n",
    "stop_time = time.time()\n",
    "print(\"结束的时间：\",stop_time)\n",
    "print(\"运行的时间：\",stop_time - start_time)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cca3baba",
   "metadata": {},
   "source": [
    "> 3. 集合的差集(考虑先后顺序)\n",
    ">> 1. 使用差集的方法：set.difference(set_2)      \n",
    ">> 2. 使用逻辑运算符：set_1 - set_2   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "1c3ad1b8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1: {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2: {0, 1, 66, 3, 4, 7, 8}\n",
      "差集结果： {2, 5, 6}\n"
     ]
    }
   ],
   "source": [
    "print(\"set_1:\",set_1)\n",
    "print(\"set_2:\",set_2)\n",
    "print(\"set_1和set_2差集结果：\",set_1.difference(set_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "b79a9274",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1: {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2: {0, 1, 66, 3, 4, 7, 8}\n",
      "set_2和set_1差集结果： {0, 8, 66}\n"
     ]
    }
   ],
   "source": [
    "print(\"set_1:\",set_1)\n",
    "print(\"set_2:\",set_2)\n",
    "print(\"set_2和set_1差集结果：\",set_2.difference(set_1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "72300ab2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1: {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2: {0, 1, 66, 3, 4, 7, 8}\n",
      "set_1和set_2差集结果： {2, 5, 6}\n"
     ]
    }
   ],
   "source": [
    "print(\"set_1:\",set_1)\n",
    "print(\"set_2:\",set_2)\n",
    "print(\"set_1和set_2差集结果：\",set_1-set_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "71ac592e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1: {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2: {0, 1, 66, 3, 4, 7, 8}\n",
      "set_1和set_2差集结果： {0, 8, 66}\n"
     ]
    }
   ],
   "source": [
    "print(\"set_1:\",set_1)\n",
    "print(\"set_2:\",set_2)\n",
    "print(\"set_1和set_2差集结果：\",set_2-set_1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "90723f83",
   "metadata": {},
   "source": [
    "> 4. 对称差集   \n",
    ">> 1. 用对称差集的方法：set_1.symmetric_difference(set_2)   \n",
    ">> 2. 使用逻辑运算符：？ (set_1-set_2) | (set_2-set_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "3cc69024",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function symmetric_difference:\n",
      "\n",
      "symmetric_difference(...) method of builtins.set instance\n",
      "    Return the symmetric difference of two sets as a new set.\n",
      "    \n",
      "    (i.e. all elements that are in exactly one of the sets.)\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set_1.symmetric_difference)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "ea261f27",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1: {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2: {0, 1, 66, 3, 4, 7, 8}\n",
      "对称差集结果： {0, 66, 2, 5, 6, 8}\n",
      "对称差集结果： {0, 2, 66, 5, 6, 8}\n"
     ]
    }
   ],
   "source": [
    "print(\"set_1:\",set_1)\n",
    "print(\"set_2:\",set_2)\n",
    "print(\"对称差集结果：\",set_1.symmetric_difference(set_2))\n",
    "print(\"对称差集结果：\",(set_1-set_2) | (set_2-set_1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "c2497726",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "vowels: {'e', 'a', 'i', 'o', 'u'}\n",
      "Provide a word to search for vowels:hello\n",
      "word: {'h', 'e', 'l', 'o'}\n",
      "{'e', 'o'}\n"
     ]
    }
   ],
   "source": [
    "#集合实践（P129）\n",
    "vowels = set('aeiou')\n",
    "print(\"vowels:\",vowels)\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "print(\"word:\",set(word))\n",
    "found = vowels.intersection(set(word))\n",
    "print(found)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a4c0421",
   "metadata": {},
   "source": [
    "# 课后作业：\n",
    "\n",
    "> 1. 课堂练习(已完成)\n",
    "> 2. 课本第三章 课后项目练习(已完成)\n",
    "> 3. 总结集合方法(已完成)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "deda7ca8",
   "metadata": {},
   "source": [
    "|序号|集合的方法|用法|使用结果|注意事项|\n",
    "|---|-----|-----|-----|----|\n",
    "|1|s.add( x )|将元素 x 添加到集合 s 中，如果元素已存在，则不进行任何操作| Add an element to a set|This has no effect if the element is already present|\n",
    "|2|s.pop( x )|随机删除集合中的一个元素|Remove and return an arbitrary set element|Raises KeyError if the set is empty|\n",
    "|3|s.remove( x )|将元素 x 从集合 s 中移除，如果元素不存在，则会发生错误|Remove an element from a set; it must be a member|If the element is not a member, raise a KeyError|\n",
    "|4|s.discard( x )|移除集合中的元素，且如果元素不存在，不会发生错误|Remove an element from a set if it is a member| If the element is not a member, do nothing|\n",
    "|5|clear()|移除集合中的所有元素|Remove all elements from this set||\n",
    "|6|copy()|拷贝一个集合| Return a shallow copy of a set||\n",
    "|7|difference()|返回多个集合的差集| Return the difference of two or more sets as a new set||\n",
    "|8|intersection()|返回集合的交集|Return the intersection of two sets as a new set||\n",
    "|9|isdisjoint()|判断两个集合是否包含相同的元素，如果没有返回 True，否则返回 False|Return True if two sets have a null intersection||\n",
    "|10|issubset()|判断指定集合是否为该方法参数集合的子集|Report whether another set contains this set||\n",
    "|11|issuperset()|判断该方法的参数集合是否为指定集合的子集|Report whether this set contains another set||\n",
    "|12|symmetric_difference()|返回两个集合中不重复的元素集合|Return the symmetric difference of two sets as a new set||\n",
    "|13|union()|返回两个集合的并集|Return the union of sets as a new set|\n",
    "|14|update()|给集合添加元素|Update a set with the union of itself and others||"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e5c7f47b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
